package org.datastructure.secondday.binarysrottree;

import java.time.temporal.ValueRange;

public class BinarySortTreeDemo {

    public static void main(String[] args) {
        int[] arr = {7, 3, 10, 12, 5, 1, 9, 2};
        BinarySortTree binarySortTree = new BinarySortTree();
        //循环的添加结点到二叉排序树
        for (int i = 0; i < arr.length; i++) {
            binarySortTree.add(new Node(arr[i]));
        }

        //中序遍历二叉排序树
        System.out.println("中序遍历二叉排序树~");
        binarySortTree.infixOrder(); // 1,2， 3, 5, 7, 9, 10, 12


        binarySortTree.delNode(12);


        binarySortTree.delNode(5);
        binarySortTree.delNode(10);
        binarySortTree.delNode(2);
        binarySortTree.delNode(3);

        binarySortTree.delNode(9);
        binarySortTree.delNode(1);
        binarySortTree.delNode(       7);




        System.out.println("删除结点后");
        binarySortTree.infixOrder();

    }
}

class BinarySortTree {
    Node root;


    public void delNode(int value) {

        if (root == null) {
            return;
        }

        Node target = search(value);
        if (target == null) {
            return;
        }

        // TODO: target不为空，且root左右没有节点时，说明root就是target 直接置空就可以
        if (root.left == null && root.right == null) {
            root = null;
            return;
        }

        // TODO: 2022/1/17 下边处理 被删除节点左右全为空、左右节点都有值，和只有一个节点
        //  还有一种是父节点为空，只有一个子节点的，要再步骤三处理过程过考虑，刚好包含parent为空，target只有一个节点
        Node parent = searchParent(value);
        // TODO: 2022/1/17 步骤一：目标节点的子节点全部为空
        if (target.left == null && target.right == null) {
            if (parent.left != null && parent.left.value == value) {
                parent.left = null;
                return;
            } else if (parent.right != null & parent.right.value == value) {
                parent.right = null;
                return;
            }
            // TODO: 2022/1/17 步骤二：目标节点的左右子节点都右值
        } else if (target.left != null && target.right != null) {
            int min = delRightMinTree(target.right);
            target.value = min;
            // TODO: 2022/1/17 步骤三：目标节点只有一个子节点
        } else {
            if (target.left != null) {// TODO: 2022/1/17 如果左子节点不为空就判断目标节点是父节点的左节点还是右节点，
                if (parent != null) {// TODO: 当目标节点是root节点时， 左右节点权为空已经处理，全不为空也已经处理，只有目标节点时root，且只有一个子节点未处理
                    //todo 如果是左子节点，就将目标节点的左子节点赋值给parent左子节点
                    if (parent.left.value == value) {
                        parent.left = target.left;
                    } else {// TODO: 2022/1/17 否则就赋给父节点的右子节点
                        parent.right = target.left;
                    }
                } else {
                    root = target.left;
                }
            } else {
                if (parent != null) {
                    if (parent.left.value == value) {
                        parent.left = target.right;
                    } else {
                        parent.right = target.right;
                    }
                } else {
                    root = target.right;
                }
            }
        }
    }


    public int delRightMinTree(Node node) {
        Node target = node;
        while (target.left != null) {
            target = target.left;
        }
        delNode(target.value);
        return target.value;
    }


    public Node searchParent(int value) {
        if (root == null) {
            return null;
        } else {
            return root.searchParent(value);
        }
    }

    public Node search(int value) {
        if (root == null) {
            return null;
        } else {
            return root.search(value);
        }
    }

    public void infixOrder() {
        if (root == null) {
            return;
        } else {
            root.infixOrd();
        }
    }

    public void add(Node node) {
        if (root == null) {
            root = node;
        } else {
            root.add(node);
        }
    }
}

class Node {
    int value;
    Node left;
    Node right;

    public Node(int value) {
        this.value = value;
    }

    public Node searchParent(int value) {
        if (this.left != null && this.left.value == value || this.right != null && this.right.value == value) {
            return this;
        } else {
            if (this.value > value && this.left != null) {
                return this.left.searchParent(value);
            } else if (value >= this.value && this.right != null) {
                return this.right.searchParent(value);
            } else {
                return null;
            }
        }
    }


    public Node search(int value) {
        if (this.value == value) {
            return this;
        }
        if (this.value > value) {
            if (this.left != null) {
                return this.left.search(value);
            } else {
                return null;
            }
        } else {
            if (this.right != null) {
                return this.right.search(value);
            } else {
                return null;
            }
        }
    }

    public void infixOrd() {
        if (this.left != null) {
            this.left.infixOrd();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.infixOrd();
        }
    }

    public void add(Node node) {
        if (node == null) {
            return;
        }

        if (node.value < this.value) {
            if (this.left == null) {
                this.left = node;
            } else {
                this.left.add(node);
            }
        } else {
            if (this.right == null) {
                this.right = node;
            } else {
                this.right.add(node);
            }
        }
    }

    @Override
    public String toString() {
        return "Node{" + "value=" + value + '}';
    }
}
